<template>
  <a-modal
    centered
    title="提交优秀商标名"
    v-model="submitVisible"
    :width="400"
    :destroyOnClose="true"
    @ok="submitTrademark"
  >
    <a-form-model
      ref="trademarkForm"
      :model="trademarkForm"
      :rules="trademarkRules"
    >
      <a-alert show-icon type="warning" message="警告" description="注意：一个商标名多个类别，请多次提交，官方建议一个商标最多两个类" />
      <a-form-model-item label="商标类别" prop="category">
        <a-select v-model="trademarkForm.category" placeholder="选择商标类别">
          <a-select-option v-for="(item,index) in categoryList" :key="index" :value="index">{{ item }}</a-select-option>
        </a-select>
      </a-form-model-item>
      <a-form-model-item label="商标名称" prop="tm_name">
        <a-input v-model="trademarkForm.tm_name" type="text" placeholder="请输入商标名称" />
      </a-form-model-item>
      <a-form-model-item label="推荐理由" prop="notes">
        <a-textarea :rows="4" v-model="trademarkForm.notes" placeholder="请输入推荐理由"></a-textarea>
      </a-form-model-item>
    </a-form-model>
  </a-modal>
</template>

<script>
import { getAddTrademark, postAddTrademark } from '@/api/adviser'

export default {
  name: 'AddTrademark',
  data () {
    return {
      submitVisible: false,
      categoryList: {},
      trademarkForm: {},
      trademarkRules: {
        category: [{ required: true, message: '请选择商标类型', trigger: 'change' }],
        tm_name: [{ required: true, message: '请输入商标名称', trigger: 'blur' }],
        notes: [{ required: true, message: '请输入推荐理由', trigger: 'blur' }]
      }
    }
  },
  created () {
    this.addTrademark()
  },
  methods: {
    submitTrademark () {
      const app = this
      app.$refs.trademarkForm.validate(valid => {
        if (valid) {
          const data = {}
          data['trade[tm_name]'] = app.trademarkForm.tm_name
          data['trade[category]'] = app.trademarkForm.category
          data['trade[notes]'] = app.trademarkForm.notes
          postAddTrademark(data).then(res => {
            if (res.status === 0) {
              app.submitVisible = false
              app.trademarkForm = {}
            } else {
              app.$warning({
                title: res.msg
              })
            }
          })
        } else {
          return false
        }
      })
    },
    addTrademark () {
      const app = this
      getAddTrademark().then(res => {
        if (res.status === 0) {
          app.categoryList = res.result.trade_list
          app.submitVisible = true
        } else {
          app.$warning({
            title: res.msg
          })
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
